import mitt from '../lib/mitt/index.js'

import Cache from './cache/index.js'

import { checkStr } from './check-reg.js'

import * as Date from './date.js'
import * as platform from './platform/index' // 平台相关api
import createRouter from './router/index'

export interface RouterConfig {
  encode?: boolean
  before?: (to: any, next: () => void) => void
  after?: (arg: any) => void
  onError?: (error: any) => void
}
const router = createRouter()

export const $util = {
  ...platform, // 平台特性相关接口
  ...Date,
  checkStr,
  Cache,
  mitt: mitt(),
  setRouter({ encode = false, before, after, onError }: RouterConfig = {}) {
    router.encode = encode ? 1 : 0

    if (before) {
      router.before = before
    }
    if (after) {
      router.after = after
    }
    if (onError) {
      router.onError = onError
    }
  },
}
export type TUtil = typeof $util & typeof router
Object.setPrototypeOf($util, router)

export default $util
